﻿using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RPC;
using Qiniu.RS;
using System;
using System.Collections.Generic;

namespace QiniuDemo
{
    /// <summary>
    /// 七牛帮助类
    /// </summary>
    public class QiniuHelper
    {
        private readonly RSClient client;

        public QiniuHelper()
        {
            Qiniu.Conf.Config.Init();
            client = new RSClient();
        }

        /// <summary>
        /// 获取文件属性
        /// </summary>
        /// <param name="bucket">七牛云存储空间名</param>
        /// <param name="key">文件key</param>
        /// <returns>文件属性</returns>
        public Entry GetFileStat(string bucket, string key)
        {
            Entry entry = client.Stat(new Qiniu.RS.EntryPath(bucket, key));

            return entry.OK ? entry : null;
        }

        /// <summary>
        /// 批量获取文件属性信息
        /// </summary>
        /// <param name="bucket">七牛云存储空间名</param>
        /// <param name="keys">文件key</param>
        /// <returns>文件属性列表</returns>
        public List<BatchRetItem> BatchStat(string bucket, string[] keys)
        {
            List<EntryPath> EntryPaths = new List<EntryPath>();
            foreach (string key in keys)
            {
                EntryPaths.Add(new EntryPath(bucket, key));
            }
            return client.BatchStat(EntryPaths.ToArray());
        }

        /// <summary>
        /// 复制单个文件
        /// </summary>
        /// <param name="bucketSrc">需要复制的文件所在的空间名</param>
        /// <param name="keySrc">需要复制的文件key</param>
        /// <param name="bucketDest">目标文件所在的空间名</param>
        /// <param name="keyDest">标文件key</param>
        public bool Copy(string bucketSrc, string keySrc, string bucketDest, string keyDest)
        {
            try
            {
                CallRet ret = client.Copy(new EntryPathPair(bucketSrc, keySrc, bucketDest, keyDest));

                return ret.OK;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 移动单个文件
        /// </summary>
        /// <param name="bucketSrc">需要移动的文件所在的空间名</param>
        /// <param name="keySrc">需要移动的文件</param>
        /// <param name="bucketDest">目标文件所在的空间名</param>
        /// <param name="keyDest">目标文件key</param>
        public bool Move(string bucketSrc, string keySrc, string bucketDest, string keyDest)
        {
            try
            {
                new EntryPathPair(bucketSrc, keySrc, bucketDest, keyDest);
                CallRet ret = client.Move(new EntryPathPair(bucketSrc, keySrc, bucketDest, keyDest));

                return ret.OK;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量移动文件
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public bool BatchMove(string bucket, string[] keys)
        {
            try
            {
                List<EntryPathPair> pairs = new List<EntryPathPair>();
                foreach (string key in keys)
                {
                    EntryPathPair entry = new EntryPathPair(bucket, key, Guid.NewGuid().ToString());
                    pairs.Add(entry);
                }
                client.BatchMove(pairs.ToArray());

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 删除单个文件
        /// </summary>
        /// <param name="bucket">文件所在的空间名</param>
        /// <param name="key">文件key</param>
        public bool Delete(string bucket, string key)
        {
            try
            {
                CallRet ret = client.Delete(new EntryPath(bucket, key));

                return ret.OK;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public bool BatchDelete(string bucket, string[] keys)
        {
            try
            {
                List<EntryPath> EntryPaths = new List<EntryPath>();
                foreach (string key in keys)
                {
                    Console.WriteLine("\n===> Stat {0}:{1}", bucket, key);
                    EntryPaths.Add(new EntryPath(bucket, key));
                }
                client.BatchDelete(EntryPaths.ToArray());

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string PutFile(string bucket, string key, string fname)
        {
            try
            {
                var policy = new PutPolicy(bucket, 3600);
                string upToken = policy.Token();
                PutExtra extra = new PutExtra();
                IOClient client = new IOClient();
                var putret = client.PutFile(upToken, key, fname, extra);

                return putret.key;
            }
            catch (Exception ex)
            {
                return "Error";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="key"></param>
        /// <param name="fname"></param>
        public static void ResumablePutFile(string bucket, string key, string fname)
        {
            PutPolicy policy = new PutPolicy(bucket, 3600);
            string upToken = policy.Token();
            Settings setting = new Settings();
            ResumablePutExtra extra = new ResumablePutExtra();
            ResumablePut client = new ResumablePut(setting, extra);
            client.PutFile(upToken, fname, Guid.NewGuid().ToString());
        }

        /// <summary>
        /// 批量复制文件
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public bool BatchCopy(string bucket, string[] keys)
        {
            try
            {
                List<EntryPathPair> pairs = new List<EntryPathPair>();
                foreach (string key in keys)
                {
                    EntryPathPair entry = new EntryPathPair(bucket, key, Guid.NewGuid().ToString());
                    pairs.Add(entry);
                }
                RSClient client = new RSClient();
                client.BatchCopy(pairs.ToArray());

                return true;
            }
            catch (Exception ex)
            {
                return true;
            }
        }
    }
}
